package main

import (
	"awesomeProject2/compare"
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {

	//计算数量()
	compare.Compare()
	//compare.CompareNewProduct()
	//mtsMt := mt.GetFileMt()
	//
	//for _, productMt := range mtsMt {
	//
	//	for _, product := range productMt.Data.ProductList {
	//		download.Download(product.Pictures[0], product.Name)
	//	}
	//
	//}

	//copyImg()

	//lines := product.OpenNameLine("C:\\Users\\Admin\\go\\src\\awesomeProject2\\mt\\png.txt")
	//names := product.OpenNameLine("C:\\Users\\Admin\\go\\src\\awesomeProject2\\mt\\activity.txt")
	//
	//for index, line := range lines[0:20] {
	//
	//	download.Download(line, names[index])
	//}

	//elms := GetFileElm()
	//
	//for _, elm := range elms[1:2] {
	//	for _, datum := range elm.Data.Data {
	//		download.Download(datum.PicURL, datum.Title)
	//	}
	//}

	//maps := make(map[string]int)
	//
	//mtsMt := mt.GetFileMt()
	//for _, products := range mtsMt {
	//	for _, datum := range products.Data.ProductList {
	//		title := name.GetNameByTitle(datum.Name)
	//		count, flag := maps[title]
	//		if flag {
	//			maps[title] = count + 1
	//		} else {
	//			maps[title] = 1
	//		}
	//	}
	//}
	//
	//for s, i := range maps {
	//	if i > 1 {
	//		println(s)
	//	}
	//}

	//mt.WriteFileMt()

	//elm.WriteFileMt()

	//elm.WriteFileElm()

	//
	//for _, name := range names {
	//	println(name)
	//}

	//getTxmMei()
	//getTxmMei2()

	//products := product.GetProductMt()
	//
	//for _, s := range products.Data.ProductList {
	//	imagPath := s.Pictures[0]
	//
	//	name := getNameByTitle(s.Name)
	//	if name != "" {
	//		//通过http请求获取图片的流文件
	//		resp, _ := http.Get(imagPath)
	//		body, _ := ioutil.ReadAll(resp.Body)
	//		out, _ := os.Create("png/" + name + ".png")
	//		io.Copy(out, bytes2.NewReader(body))
	//	}
	//}

	//product := openFile()
	//
	//names := openName()

	//条形码
	//getTxm()
	//库存
	//getStock()

	//for _, name := range names {
	//	price := "0.00"
	//	for _, datum := range product.Data.Data {
	//		start := strings.Index(datum.Title, "【")
	//		end := strings.Index(datum.Title, "】")
	//		s2 := datum.Title[start+3 : end]
	//
	//		if s2 == name {
	//			//s := fmt.Sprintf("name = %s,value = %.2f", s2, datum.MinActivePrice)
	//			price = fmt.Sprintf("%.2f", datum.MinActivePrice)
	//		}
	//	}
	//	println(price)
	//}
}

func 计算数量() {
	fileHandle, err := os.OpenFile("C:\\Users\\admin\\GolandProjects\\waimaigo\\现在的价格", os.O_RDONLY, 0666)
	if err != nil {
		return
	}

	defer fileHandle.Close()

	readBytes, err := ioutil.ReadAll(fileHandle)
	if err != nil {
		return
	}

	results := strings.Split(string(readBytes), "\n")

	m := make(map[string]int)

	for _, result := range results {
		//11牌优选【怦然心动】11枝香槟玫瑰花束小香风花束生日鲜花生日礼物生日花束x1
		i1 := strings.Index(result, "【")
		i2 := strings.Index(result, "】")
		if i1 > 0 {
			r := result[i1+3 : i2]
			c, _ := m[r]
			c = c + 1
			m[r] = c
		}
	}

	for s, i := range m {
		fmt.Printf("%s-%d\n", s, i)
	}
}

func copyImg() {
	readDir := "D:\\png\\png4"

	dir, err := os.ReadDir(readDir)
	if err != nil {
		return
	}

	for i, entry := range dir {
		name := entry.Name()

		newName1 := strings.ReplaceAll(name, ".png", "-1.png")
		newName2 := strings.ReplaceAll(name, ".png", "-2.png")
		newName3 := strings.ReplaceAll(name, ".png", "-3.png")

		i2 := i % 10

		os.MkdirAll("D:\\png\\png5\\"+strconv.Itoa(i2), os.ModePerm)
		copyFile("D:\\png\\png5\\"+strconv.Itoa(i2)+"\\"+newName1, readDir+"\\"+entry.Name())
		copyFile("D:\\png\\png5\\"+strconv.Itoa(i2)+"\\"+newName2, readDir+"\\"+entry.Name())
		copyFile("D:\\png\\png5\\"+strconv.Itoa(i2)+"\\"+newName3, readDir+"\\"+entry.Name())

		//copyFile()

	}
}

// 编写一个函数，接收两个文件路径:
func copyFile(dstFileName string, srcFileName string) (written int64, err error) {
	srcFile, err := os.Open(srcFileName)
	if err != nil {
		fmt.Printf("open file error = %v\n", err)
	}
	defer srcFile.Close()

	//通过srcFile，获取到READER
	reader := bufio.NewReader(srcFile)

	//打开dstFileName
	dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Printf("open file error = %v\n", err)
		return
	}

	//通过dstFile，获取到WRITER
	writer := bufio.NewWriter(dstFile)
	//writer.Flush()

	defer dstFile.Close()

	return io.Copy(writer, reader)
}
